package com.sinodoggo.shitcloud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sinodoggo.shitcloud.commons.Constants;
import com.sinodoggo.shitcloud.entity.User;
import com.sinodoggo.shitcloud.entity.dto.UserDTO;
import com.sinodoggo.shitcloud.exception.ServiceException;
import com.sinodoggo.shitcloud.mapper.UserMapper;
import com.sinodoggo.shitcloud.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinodoggo.shitcloud.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sinodoggo
 * @since 2022-11-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper mapper;

    private User getUserInfo(UserDTO userDTO) {
        final String userName = userDTO.getUserName();
        final String password = userDTO.getPassword();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName);
        wrapper.eq("password", password);
        try {
            return mapper.selectOne(wrapper);
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误，请与开发者联系");
        }
    }

    @Override
    public UserDTO userLogin(UserDTO userDTO) {
        User user = this.getUserInfo(userDTO);
        if (null != user) {
            // 把查询到的用户信息保存到DTO中
            BeanUtils.copyProperties(user, userDTO);
            // 获取token，并在DTO中设置token
            final String token = TokenUtils.genToken(user.getId().toString(), user.getPassword());
            userDTO.setToken(token);
            return userDTO;
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名或密码错误!");
        }
    }

    @Override
    public UserDTO userRegist(UserDTO userDTO) {
        final User userInfo = this.getUserInfo(userDTO);
        if (null == userInfo) {
            User user = new User();
            // 奶奶滴hutools的BeanUtil里有Bug
            BeanUtils.copyProperties(userDTO, user);
            mapper.insert(user);
            return userDTO;
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名已存在!");
        }
    }

    @Override
    public UserDTO userRetrive(UserDTO userDTO) {
        final String userName = userDTO.getUserName();
        final String email = userDTO.getEmail();
        QueryWrapper<User> searchWrapper = new QueryWrapper<>();
        searchWrapper.eq("user_name", userName);
        searchWrapper.eq("email", email);
        final User user = mapper.selectOne(searchWrapper);
        if (null != user) {
            QueryWrapper<User> updateWrapper = new QueryWrapper<>();
            // 更新密码，傻逼hutools又在这里出了问题
            BeanUtils.copyProperties(userDTO, user);
            updateWrapper.eq("user_name", user.getUserName());
            updateWrapper.eq("email", user.getEmail());
            mapper.update(user, updateWrapper);
            return userDTO;
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名或邮箱错误!");
        }
    }
}
